﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace WPFLoaders
{
    /// <summary>Converts from a index (0 or more) to a duration in seconds where the index multiplies the duration specified by converter parameters</summary>
    [ValueConversion(typeof(int), typeof(Duration))]
    public class DelayStartConverter : IValueConverter
    {
        private static readonly DelayStartConverter SingletonInstance = new DelayStartConverter();

        public static DelayStartConverter Instance { get { return SingletonInstance; } }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var delayTime = (double) System.Convert.ChangeType(parameter, typeof (double));
            int index = (int) value;

            return new Duration(TimeSpan.FromSeconds(delayTime*index));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("Conversion from duration to index is not supported");
        }
    }
}
